package com.vnp.cooking;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.KeyboardHelper;
import com.vnp.cooking.utils.PreferenceHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class TaoMonAnNhapTenActivity extends Activity implements OnItemClickListener,OnClickListener, OnEditorActionListener{
	
	ImageButton btnBack, btnNextStep;
	EditText edtNhapten;
	// Object mon an
    ObjCookpad objCookpad = new ObjCookpad();
    ArrayList<String> tenMonAns = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    PreferenceHelper preferenceHelper;
    View vFocus;
    ImageView btnClear;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nhapten);
		preferenceHelper = new PreferenceHelper(this);
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnNextStep = (ImageButton) findViewById(R.id.btn_next_step);
		edtNhapten = (EditText) findViewById(R.id.edt_ten_mon);
		vFocus = (View) findViewById(R.id.v_focus);
		btnClear = (ImageView) findViewById(R.id.btn_clear);
//		adapter = new ArrayAdapter<String>(TaoMonAnNhapTenActivity.this, android.R.layout.simple_list_item_1, createData());
		
		
		init();
	}
	
	void init() {
		KeyboardHelper.displaySoftKeyboard(this, edtNhapten);
		btnBack.setOnClickListener(this);
		btnNextStep.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		
		// xu li khi nhap ten mon an
		edtNhapten.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_WORDS);
		edtNhapten.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				btnClear.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().length() > 0) {
					
				} else {
					btnClear.setVisibility(View.GONE);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_next_step:
			KeyboardHelper.hideSoftKeyboard(TaoMonAnNhapTenActivity.this);
			checkNextStep();
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		KeyboardHelper.hideSoftKeyboard(TaoMonAnNhapTenActivity.this);
		checkNextStep();
		return true;
	}

	
	// check buoc di buoc tiep theo
		public void checkNextStep() {
			if (TextUtils.isEmpty(edtNhapten.getText().toString())) {
				Toast.makeText(TaoMonAnNhapTenActivity.this, "Bạn chưa nhập tên món ăn", Toast.LENGTH_SHORT).show();
			} else {
				createTaomoi();
				objCookpad.setNameCookpad(edtNhapten.getText().toString());
				// truyen du lieu tu ben chup anh sang
				Intent t = new Intent(TaoMonAnNhapTenActivity.this, DangCongThucActivity.class);
				t.putExtra("objCookpad", objCookpad);
				startActivity(t);
			}
		}

		@Override
		public void onItemClick(AdapterView<?> adapterView, View v, int position,
				long id) {
//			edtNhapten.setText(""+createData().get(position));
		}
		
		
		// tao cong thuc moi chua co thong tin
		public void createTaomoi(){
			HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

				@Override
				public void onFailure(Throwable error) {
					super.onFailure(error);
				}

				@Override
				public void onSuccess(JSONObject re) {
					try {
						int status = re.getInt("status");
						if (status == 200) {
							JSONObject data = re.getJSONObject("data");
							String recipeId = data.getString("recipe_id");
							objCookpad.setId(recipeId);
							preferenceHelper.setRecipeId(recipeId);
							Log.i("DATA", "RECIPE DATA: "+data.toString());
							Log.i("DATA", "RECIPE SAVE: "+preferenceHelper.getRecipeId());
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				
			});
			httpClientHelper.getCreateCT(preferenceHelper.getID());
		}

}
